//
//  BookStoreViewController.m
//  LineByLine
//
//  Created by 병철 김 on 11. 10. 18..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BookStoreBookViewController.h"
#import "QHTTPOperation.h"
#import "NetworkManager.h"
#import "BookParserDelegate.h"
#import "CoreDataManager.h"


#define kDownScript 1

@implementation BookStoreBookViewController
@synthesize bookId;

- (void)loadView {
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *downScript = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton *downMedia = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [downScript setTitle:@"downScript" forState:UIControlStateNormal];
    [downMedia setTitle:@"downMedia" forState:UIControlStateNormal];
    
    [downScript setFrame:CGRectMake(10, 50, 100, 30)];
    [downMedia setFrame:CGRectMake(10, 100, 100, 30)];
    
    [self.view addSubview:downScript];
    [self.view addSubview:downMedia];
    
    [downScript addTarget:self action:@selector(onClickDownScript) forControlEvents:UIControlEventTouchUpInside];
    [downMedia addTarget:self action:@selector(onClickDownMedia) forControlEvents:UIControlEventTouchUpInside];
}

- (void) onClickDownScript {
    if (downScriptHttpRequest) {
        [downScriptHttpRequest close];
        downScriptHttpRequest = nil;
    }
    
    downScriptHttpRequest = [[HttpRequest alloc] init];
    [downScriptHttpRequest setFinishTarget:self action:@selector(didDownScript:)];
    [downScriptHttpRequest executeUrl:[NSString stringWithFormat:@"http://linebylinestore.appspot.com/book/%d.xml", [bookId intValue]] method:@"GET" params:nil];
}

- (void) onClickDownMedia {
    if (downMediaHttpRequest) {
        [downMediaHttpRequest close];
        downMediaHttpRequest = nil;
    }
    
    downMediaHttpRequest = [[HttpRequest alloc] init];
    [downMediaHttpRequest setFinishTarget:self action:@selector(didDownMedia:)];
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Library/Caches/%d.mp3", [bookId integerValue]]];
    NSOutputStream *outputStream = [[NSOutputStream alloc] initToFileAtPath:filePath append:NO];
    [downMediaHttpRequest setOutputStream:outputStream];
    [downMediaHttpRequest executeUrl:[NSString stringWithFormat:@"http://linebylinestore.appspot.com/media/%d.mp3", [bookId intValue]] method:@"GET" params:nil];
}

- (void) parseScript:(NSData*) responseData {
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:responseData];
    BookParserDelegate *bookParser = [[BookParserDelegate alloc] init];
    id book = [[CoreDataManager sharedCoreDataManager] insertOneInto:@"Book" values:@"bookId", [NSNumber numberWithInt:[bookId intValue]], nil];
    [bookParser setBook:book];
    [bookParser setContext:[[CoreDataManager sharedCoreDataManager] managedObjectContext]];
    [xmlParser setDelegate:bookParser];
    
    NSAutoreleasePool *tempPool = [[NSAutoreleasePool alloc] init];
    [xmlParser parse];
    
    [tempPool drain];
    [bookParser release];
    [xmlParser release];
}

- (void) didDownScript:(HttpRequest*) httpRequest {
    NSData *responseData = [[downScriptHttpRequest responseData] retain];
    [downScriptHttpRequest release];
    downScriptHttpRequest = nil;
    
    [self parseScript:responseData];
    [responseData release];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Down Complete" message:@"Down Complete~" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

- (void) didDownMedia:(HttpRequest*) httpRequest {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Down Mp3 Complete" message:@"Down Mp3 Complete~" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

@end
